package com.common.ssm.sql.service.impl;

import com.common.ssm.sql.mapper.MakerwitSqlMapper;
import com.common.ssm.sql.service.MakerwitSqlService;
import com.common.utils.MakerwitBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class MakerwitSqlServiceImpl implements MakerwitSqlService {

    @Autowired
    private MakerwitSqlMapper zs;

    @Override
    public int insertTabNameByColumns(String tabName, Map columns) {
        return zs.insertTabNameByColumns(tabName, columns);
    }

    @Override
    public int insertBatch(String tabName, List<Map> list, boolean flag) {
        return zs.insertBatchAll(tabName, list, true);
    }

    @Override
    public int deleteTabByWhereMap(String tabName, Map where) {
        // TODO Auto-generated method stub
        //SqlUtil.isValidSqlMapParam(where);
        return zs.deleteTabByWhereMap(tabName, where);
    }

    @Override
    public int updateTabBySetWhereMap(String tabName, Map set, Map where) {
        return zs.updateTabBySetWhereMap(tabName, set, where);
    }

    /**
     * 通过表名、id获取表数据 若id=0 查询表全部数据
     */
    @Override
    public List<?> selectById(Class<?> c, String tabName, int id) {
        List<Map<String, Object>> m =  zs.selectTabNameById(tabName, id);
        List ret = new ArrayList();
        if(m.size()>0){
            for (int i = 0; i < m.size(); i++) {
                try {
//                    ret.add(ModelUtil.mapToBean(c, m.get(i), false));
//                    BeanUtils.populate(c, m.get(i));
                    //TODO MAP to Bean（三种）     modelUtil 自创   BeanUUtils   下面
                    //拿到  BeanInfo
                    BeanInfo beanInfo = Introspector.getBeanInfo(c.getClass());

                    //通过 beaninfo 获取所有的描述器
                    PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();

                    for (PropertyDescriptor pd : pds) {

                        //获取属性的名字
                        String name = pd.getName();

                        Map map = m.get(i);
                        //判断是否有这个属性
                        if (map.containsKey(name)) {
                            //获取属性的  写的方法
                            Method wMethod = pd.getWriteMethod();

                            if (pd.getPropertyType() == int.class) {
                                ret.add(wMethod.invoke(c, Integer.valueOf((Integer) map.get(name))));

                            } else if (pd.getPropertyType() == double.class) {

                                ret.add(wMethod.invoke(c, Double.valueOf((Double) map.get(name))));

                            } else {
                                ret.add(wMethod.invoke(c, map.get(name)));
                            }
                        }

                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        return ret;
    }

    /**
     * 条件查询
     * @param tabName 表名
     * @param where Map条件 key与表中字段名一致 支持like、>、>=、<、<=、模糊查询： like %查询内容% 、in (查询内容)
     * @return
     */
    //    MakerwitBean where = MakerwitBean.create().set("ID", "in (".concat(ids).concat(")"));
    //    List<Map<String, Object>> allotpost = ms.select("u_allot_post", where);
    @Override
    public List<Map<String, Object>> select(String tabName, Map where) {
        // TODO Auto-generated method stub
        return zs.selectInfoByColumns(tabName, where);
    }

    @Override
    public List<Map<String, Object>> selectwhere(String tabName, String key, String symbol, String value) {
        MakerwitBean set = null;
        if (key.equals("=") || key.equals(">") || key.equals(">=") || key.equals("<") || key.equals("<=")) {
            set = MakerwitBean.create().set(key, symbol.concat(" '").concat(value).concat("'"));
        }
        return zs.selectInfoByColumns(tabName, set);
    }

    @Override
    public List<Map<String, Object>> selectwherein(String tabName, String key, String value) {
        MakerwitBean set = MakerwitBean.create().set(key,"in (".concat(value).concat(")"));
        return zs.selectInfoByColumns(tabName, set);
    }

    @Override
    public List<Map<String, Object>> selectwherelike(String tabName, String key, String value) {
        MakerwitBean set = MakerwitBean.create().set(key,"like $".concat(value).concat("$"));
        return zs.selectInfoByColumns(tabName, set);
    }
    /**
     *
     * @param tabName 表名
     * @param where Map条件 key与表中字段名一致 支持like、>、>=、<、<=、模糊查询： like %查询内容% 、in (查询内容)
     * @param sortKey 需要排序字段
     * @return
     */
    @Override
    public List<Map<String, Object>> selectsc(String tabName, Map where, String sortKey) {
        return zs.selectInfoByColumnsOrderby(tabName, where,sortKey);
    }
}
